package cn.ygh.bbo.common.utils;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author ygh
 * @Desc 通用对象工具
 * @date 2019/11/3
 */
public class ClassUtils {

    /**
     * 类型转换
     *
     * @param obj
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> T convet(Object obj, Class<T> tClass) {
        T t = null;
        try {
            t = tClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            return null;
        }
        BeanUtils.copyProperties(obj, t);
        return t;
    }

    /**
     * 类型转换
     *
     * @param list
     * @param tClass
     * @param <T>
     * @return
     */
    public static <T> List<T> convetList(List list, Class<T> tClass) {
        return (List<T>) list.stream().map(e -> convet(e, tClass)).collect(Collectors.toList());
    }

    /**
     * 两个对象比较，相同字段，值不一样的话，就放到MAP返回
     *
     * @param obj1
     * @param obj2
     * @return Map[String, Object[]] Map[fieldname,[obj1.value,obj2.value]]
     */
    public static Map<String, Object[]> compare2obj(Object obj1, Object obj2) throws IllegalAccessException {
        Map<String, Object[]> ret = new HashMap<String, Object[]>();
        //其中一个为空
        if (obj1 == null || obj2 == null) return ret;
        Field[] fields1 = obj1.getClass().getDeclaredFields();
        Field[] fields2 = obj2.getClass().getDeclaredFields();
        //对比两个对象的属性
        for (Field field1 : fields1) {
            for (Field field2 : fields2) {
                if (field1.getName().equals(field2.getName())) {
                    Object o1 = FieldUtils.readField(field1, obj1, true);
                    Object o2 = FieldUtils.readField(field2, obj2, true);
                    if (ObjectUtils.notEqual(o1, o2)) {
                        ret.put(field1.getName(), new Object[]{o1, o2});
                    }
                    break;
                }
            }
        }
        return ret;
    }

    /**
     * 对象属性设置
     *
     * @param obj
     * @param fieldName
     * @param value
     * @throws IllegalAccessException
     */
    public static void setFieldValue(Object obj, String fieldName, Object value) throws IllegalAccessException {
        Field field = FieldUtils.getField(obj.getClass(), fieldName, true);
        if (field != null) {
            FieldUtils.writeField(field, obj, value, true);
        }
    }

    /**
     * 对象属性获取
     *
     * @param obj
     * @param fieldName
     * @return
     * @throws IllegalAccessException
     */
    public static Object getFieldValue(Object obj, String fieldName) throws IllegalAccessException {
        Field field = FieldUtils.getField(obj.getClass(), fieldName, true);
        if (field != null) {
            return FieldUtils.readField(field, obj, true);
        }
        return null;
    }
}
